﻿/*-----------------------------------------------
// File: SkillAudioCtrl.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class SkillAudioCtrl 
    {
        public BattleSkill ownSkill;

        bool isRunning = false;
        SkillAudioClip currentTask = null;
        Queue<SkillAudioClip> leftAudioTasks = new Queue<SkillAudioClip>();
        float currentTimeLine = 0f;
        List<AudioSource> cachedAudioSource = new List<AudioSource>();

        public SkillAudioCtrl(BattleSkill skill) {
            this.ownSkill = skill;
            isRunning = false;
        }

        public bool IsPlaying() {
            foreach (var a in cachedAudioSource) {
                if (a != null && a.isPlaying)
                    return true;
            }
            return false;
        }

        public void Tick() {
            if (isRunning) {
                currentTimeLine += Time.deltaTime;
                if (currentTask == null) {
                    if (leftAudioTasks.Count > 0)
                    {
                        currentTask = leftAudioTasks.Dequeue();
                    }
                    else {
                        if (!IsPlaying()) {
                            Stop();
                        }
                    }
                }
                if (currentTask != null) {
                    if (currentTask.timePoint <= currentTimeLine) {
                        var audioSource = AudioManager.Instance.PlaySingle(currentTask.audioName);
                        currentTask = null;
                        cachedAudioSource.Add(audioSource);
                    }
                }
            }
        }

        public void Start(string audioInfo) {
            currentTask = null;
            leftAudioTasks = new Queue<SkillAudioClip>();
            cachedAudioSource = new List<AudioSource>();
            var taskList = new List<SkillAudioClip>();
            string[] arr = audioInfo.Split( new char[1] { '#' }, System.StringSplitOptions.RemoveEmptyEntries );
            foreach (var info in arr) {
                var arr0 = info.Split(new char[1] { '=' }, System.StringSplitOptions.RemoveEmptyEntries);
                if (arr0.Length == 2) {
                    string audioName = arr0[0];
                    float playTime = 0f;
                    if (!float.TryParse(arr0[1], out playTime)) {
                        Debug.LogError("[AudioInfo Error] SkillId:" + ownSkill.HSId + " info:" + info);
                    }
                    SkillAudioClip clip = new SkillAudioClip(audioName, playTime);
                    taskList.Add(clip);
                }
            }
            taskList.Sort(delegate (SkillAudioClip x, SkillAudioClip y) {
                return x.timePoint.CompareTo(y.timePoint);
            });
            foreach (var task in taskList) {
                leftAudioTasks.Enqueue(task);
            }
            currentTimeLine = 0f;
            isRunning = true;
        }

        public void Stop() {
            currentTask = null;
            leftAudioTasks.Clear();
            isRunning = false;
            currentTimeLine = 0f;
            foreach (var a in cachedAudioSource) {
                if (a != null) {
                    a.Stop();
                }
            }
            cachedAudioSource.Clear();
        }
    }

    public class SkillAudioClip {
        public string audioName;
        public float timePoint;

        public SkillAudioClip(string _audioName, float _time) {
            this.audioName = _audioName;
            this.timePoint = _time;
        }
    }
}